## -*- coding: utf-8 -*-
<%!
    from pythonblog.service.content_service import is_user_digged
%>

<%def name="flash()">
    % if session.has_key('flash_msg'):
    <div class="flash_notice"><p>${h.literal(session.get('flash_msg'))}</p></div>
    <%
    del session['flash_msg']
    session.save()
    %>
    % endif
</%def>

<%def name="digg(post)">
<div class="digg" id="d${post.id}">
    % if is_user_digged(post.id, h.get_user_identifier()):
    <h3 class="digged"><span class="digg">${post.promotes_count}顶</span><br/><span class="bury">${post.demotes_count}踩</span></h3>
    % else:
     <h3><a href="#" onclick='digg(${post.id}, 1);return false;' class="digg">${post.promotes_count}顶</a><br/><a href="#" onclick='digg(${post.id}, 0);return false;' class="bury">${post.demotes_count}踩</a></h3>
    % endif
</div>
</%def>

<%def name="digg_bottom(post)">
    % if is_user_digged(post.id, h.get_user_identifier()):
    <div class="digged"><strong>${post.promotes_count}</strong> <br/>顶</div>
    % else:
    <a onclick='digg(${post.id}, 1);return false;' href="#"><div><strong>${post.promotes_count}</strong> <br/>顶一下</div></a>
    % endif
</%def>

<%def name="more_link(section_id)">
  <div style="text-align:right;padding: 10px 20px 20px 0;">
      <a href="${h.url_for_www('section', id=section_id)}" style="color:#E28822;font-size:110%;font-weight:bold;">更多文章>></a>
  </div>
</%def>

<%def name="link_for_user(userName, title=None)">
<%
  if title is None:
    title = 'http://%s%s' % (userName, h.domain)
%>
<a href="${h.url_for('home', sub_domain=userName)}">${title}</a>
</%def>

<%def name="link_for_user_logo(user)">
<a href="${h.url_for('home', sub_domain=user.userName)}"><img title="${user.author}" src="${h.url_for(user.user_logo)}" class="logo" alt="用户头像"/></a>
</%def>

<%def name="link(url, title)">
<a href="${url}">${title}</a>
</%def>

<%def name="link_for_post(post, sub_domain='www')">
<a href="${h.url_for(controller='post', action='view', id=post.id, sub_domain=sub_domain)}" title="${post.title}">${post.title}</a>
</%def>

<%def name="link_for_comment(post_id, title, sub_domain='www')">
<a href="${h.url_for(controller='post', action='view', id=post_id, sub_domain=sub_domain)}#comments">${title}</a>
</%def>

<%def name="link_for_section(section)">
${link(h.url_for_www('section', section_path=section.path), section.name)}
</%def>

<%def name="head1(title, url)">
<div><a href="${url}" id="heading" class="head1 heading ">${title}</a></div>
</%def>

<%def name="page(index, curr_page, url, segment)">
% if index == curr_page:
  <span class="current">${index}</span> \
% else:
  <a style="text-decoration:none;" rel="next" href="${'%s?page=%s%s'% (url, index, segment)}">${index}</a> \
% endif
</%def>

<%def name="paginate(paginator, url, segment='')">
    <div class="pagination"> \
      % if paginator.page == paginator.first_page:
      <span class="disabled prev_page">« 上一页</span> \
      % else:
      <a style="text-decoration:none;" rel="next" class="prev_page" href="${'%s?page=%s%s'% (url, paginator.page-1, segment)}">« 上一页</a> \
      % endif
      % if paginator.page_count <= 8:
        % for i in range(1, paginator.page_count+1):
          ${page(i, paginator.page, url, segment)}
        % endfor
      % else:
        % for i in range(1, 3):
          ${page(i, paginator.page, url, segment)}
        % endfor
        % if paginator.page == 2:
          ${page(3, paginator.page, url, segment)}
        % endif
        % if paginator.page > 2 and paginator.page < paginator.last_page - 1:
          % if paginator.page > 4:
          <span class="gap">…</span> \
          % endif
          % for i in range(max(3, paginator.page-1), min(paginator.page+1, paginator.last_page-2) + 1):
              ${page(i, paginator.page, url, segment)}
          % endfor
          % if paginator.page < paginator.last_page-3:
            <span class="gap">…</span> \
          % endif
        % else:
          <% middle = (paginator.page_count + 1) / 2 %>
          % if paginator.page > middle or paginator.page + 2 < middle - 1:
          <span class="gap">…</span> \
          % endif
          % for i in range(middle - 1, middle + 2):
              ${page(i, paginator.page, url, segment)}
          % endfor
          % if paginator.page < middle or paginator.page - 2 > middle + 1:
          <span class="gap">…</span> \
          % endif 
        % endif
        % if paginator.page == paginator.last_page - 1:
          ${page(paginator.last_page - 2, paginator.page, url, segment)}
        % endif
        % for i in range(paginator.last_page-1, paginator.last_page+1):
          ${page(i, paginator.page, url, segment)}
        % endfor
      % endif
      % if paginator.page == paginator.last_page:
      <span class="disabled next_page">下一页 »</span> \
      % else:
      <a style="text-decoration:none;" rel="next" class="next_page" href="${'%s?page=%s%s'% (url, paginator.page+1, segment)}">下一页 »</a> \
      % endif
    </div>
</%def>